package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.QueryRoleDto;
import com.huaxiafinance.management.mapper.RoleMapper;
import com.huaxiafinance.management.mapper.RolePermissionMapper;
import com.huaxiafinance.management.model.Role;
import com.huaxiafinance.management.model.RolePermission;
import com.huaxiafinance.management.service.RoleService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

	private static final Logger log = Logger.getLogger(RoleServiceImpl.class);

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RolePermissionMapper rpMapper;

	@Transactional
	@Override
	public boolean addRole(Role role) throws Exception {
		int i = 0;
		int j = 0;

		try {
			i = roleMapper.insert(role);
			if (i > 0) {
				for (int permissId : role.getPermissIds()) {
					RolePermission rp = new RolePermission();
					rp.setRoleId(role.getId());
					rp.setPermissionId(permissId);
					j = rpMapper.insert(rp);
				}
			}

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return j > 0;
	}

	@Override
	public List<Role> queryRole(QueryRoleDto dto) throws Exception {
		dto.setPage(dto.getPage() * dto.getRows());
		
		try {
			return roleMapper.queryRole(dto);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return Collections.emptyList();
	}

	@Transactional
	@Override
	public boolean updateRole(Role role) throws Exception {
		int i = 0;
		int j = 0;
		int k = 0;
		try {
			i = roleMapper.updateRole(role);
			if (i > 0) {
				j = rpMapper.delete(role.getId());
				if (j > 0) {
					for (int permissId : role.getPermissIds()) {
						RolePermission rp = new RolePermission();
						rp.setRoleId(role.getId());
						rp.setPermissionId(permissId);
						k = rpMapper.insert(rp);
					}
				}
			}

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return k > 0;
	}

	@Transactional
	@Override
	public MessageResult delRole(Integer id) throws Exception {
		MessageResult messageResult = new MessageResult();

		try {
			int i = roleMapper.delRole(id);
			if (i > 0){
				i = rpMapper.delete(id);
				messageResult.setResultMessage(ServiceCode.SUCCESS);
			}
		} catch (DataIntegrityViolationException e) {
			messageResult.setResultMessage(ServiceCode.USER_ROLE_DEL_EXP);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
		} 

		return messageResult;
	}

	@Override
	public int countRole(QueryRoleDto dto) throws Exception {
		dto.setPage(null);
		
		try {
			return roleMapper.queryRole(dto).size();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return 0;
	}
}
